def func():
    stream = None
    try:
        stream = open(r'E:\p1\test.txt')
        container = stream.read()
        print(container)
        return 1
    except Exception as err:
        print(err)
        return 2
    finally:
        print('------finally------')
        if stream:
            stream.close()
        return 3

x = func()
print(x)

'''
抛出异常 raise

注册 用户名必须是6位
'''
def register():
    username = input('请输入用户名：')
    if len(username) < 6:
        raise Exception('用户名长度不能小于6位')
    else:
        print('输入的用户名是：',username)

try:
    register()
except Exception as err:
    print(err)
    print('注册失败')
else:
    print('注册成功')






















